package com.example.onlinetaxi.BaiDuMap.RoutePlan;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.example.onlinetaxi.BaiDuMap.BaiDuMapManager;
import com.example.onlinetaxi.LocalStroage.LocalStorage;
import com.example.onlinetaxi.callback.RouteInfoCallBack.IRouteInfo;
import com.example.onlinetaxi.LocalStroage.PassengerRouteLine;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class RoutePlan {
    // 路径起始位置(传入city和place)
    public static Map<String, String> startMap = new HashMap<>();
    public static Map<String, String> endMap = new HashMap<>();
    // 设置地图
    private MapView mMapView;
    private BaiduMap mBaiduMap;
    // 传递路径数据
    private IRouteInfo iRouteInfo;
    //创建DrivingRouteOverlay实例
    DrivingRouteOverlay overlay;
    // 司机到乘客路径
    DrivingRouteLine drivingRouteLineForDriver;
    // 乘客起点到终点路径
    DrivingRouteLine drivingRouteLineForPassenger;
    // 上下文
    public static Activity activity;

    // 构造函数
    public RoutePlan(MapView mMapView) {
        // 初始化地图
        this.mMapView = mMapView;
        mBaiduMap = mMapView.getMap();
        // 初始化覆盖物集合
        overlay = new DrivingRouteOverlay(mBaiduMap);
    }

    public void setiRouteInfo(IRouteInfo iRouteInfo) {
        this.iRouteInfo = iRouteInfo;
    }

    public static void findRoute() {

        // 创建驾车路线规划检索实例
        RoutePlanSearch mSearch = RoutePlanSearch.newInstance();

        // 创建路线规划检索结果监听器
        OnGetRoutePlanResultListener listener = new OnGetDrivingRouteResult() {
            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                if (drivingRouteResult.getRouteLines() == null && activity != null) {
                    Toast.makeText(activity, "当前路线不存在，请更换乘车起点！", Toast.LENGTH_LONG).show();
                    activity.finish();
                    return;
                }

                if (drivingRouteResult.getRouteLines().size() > 0) {
                    //获取路径规划数据,(以返回的第一条路线为例）
                    PassengerRouteLine.drivingRouteLine = drivingRouteResult.getRouteLines().get(0);

                }
            }
        };

        // 添加监听器
        mSearch.setOnGetRoutePlanResultListener(listener);
        // 获得路线的起始和结束位置
        PlanNode stNode = PlanNode.withCityNameAndPlaceName(LocalStorage.currentOrder.getStartCity(), LocalStorage.currentOrder.getStartInfo());
        PlanNode enNode = PlanNode.withCityNameAndPlaceName(LocalStorage.currentOrder.getEndCity(), LocalStorage.currentOrder.getEndInfo());

        // 发起检索
        mSearch.drivingSearch((new DrivingRoutePlanOption())
                .from(stNode)
                .to(enNode));
    }

    // 路线规划API
    public void drawRoute() {
        // 创建驾车路线规划检索实例
        RoutePlanSearch mSearch = RoutePlanSearch.newInstance();

        // 创建路线规划检索结果监听器
        OnGetRoutePlanResultListener listener = new OnGetDrivingRouteResult() {
            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                if (drivingRouteResult.getRouteLines() == null && activity != null) {
                    Toast.makeText(activity, "当前路线不存在，请更换乘车起点！", Toast.LENGTH_LONG).show();
                    activity.finish();
                    return;
                }

                if (drivingRouteResult.getRouteLines().size() > 0) {
                    //获取路径规划数据,(以返回的第一条路线为例）
                    //为DrivingRouteOverlay实例设置数据
                    drivingRouteLineForPassenger = drivingRouteResult.getRouteLines().get(0);

                    PassengerRouteLine.drivingRouteLine = drivingRouteLineForPassenger;

                    // 获取行车公里数(小数点后一位)
                    Integer distanceInt = drivingRouteLineForPassenger.getDistance();
                    DecimalFormat df = new DecimalFormat("#.0");
                    Double distance = Double.parseDouble(df.format(distanceInt / 1000.0));
                    // 获取分钟数(不足一则加一)
                    Integer minutes = (int) Math.floor(drivingRouteLineForPassenger.getDuration() / 60.0);

                    // 用来获取起点的经纬度信息
                    LatLng latLng = drivingRouteLineForPassenger.getStarting().getLocation();

                    //在地图上绘制DrivingRouteOverlay
                    overlay.addToMap(drivingRouteLineForPassenger);
                    // 按照线路缩放地图
                    overlay.zoomToSpanPaddingBounds(150, 300, 150, 1300);

                    // 设置路径信息
                    iRouteInfo.getRouteInfo(startMap.get("place"), startMap.get("city"), endMap.get("place"), endMap.get("city"), distance.toString(), minutes, latLng);
                }
            }
        };

        // 添加监听器
        mSearch.setOnGetRoutePlanResultListener(listener);
        // 获得路线的起始和结束位置
        PlanNode stNode = PlanNode.withCityNameAndPlaceName(startMap.get("city"), startMap.get("place"));
        PlanNode enNode = PlanNode.withCityNameAndPlaceName(endMap.get("city"), endMap.get("place"));

        // 发起检索
        mSearch.drivingSearch((new DrivingRoutePlanOption())
                .from(stNode)
                .to(enNode));
    }

    // 绘制小车移动
    public void drawRouteAndCar(Map<String, String> startMap, Map<String, String> endMap) {
        // 创建驾车路线规划检索实例
        RoutePlanSearch mSearch = RoutePlanSearch.newInstance();

        // 创建路线规划检索结果监听器
        OnGetRoutePlanResultListener listener = new OnGetDrivingRouteResult() {
            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
                if (drivingRouteResult.getRouteLines() == null && activity != null) {
                    Toast.makeText(activity, "当前路线不存在，请更换乘车起点！", Toast.LENGTH_LONG).show();
                    activity.finish();
                    return;
                }

                if (drivingRouteResult.getRouteLines().size() > 0) {
                    //获取路径规划数据,(以返回的第一条路线为例）
                    //为DrivingRouteOverlay实例设置数据
                    drivingRouteLineForDriver = drivingRouteResult.getRouteLines().get(0);

                    // 在地图上绘制DrivingRouteOverlay
                    // 此时drvingRouteForPassenger是空的！需要保存用户订单的路径！！
                    drivingRouteLineForPassenger = PassengerRouteLine.drivingRouteLine;
                    // 同时在地图上绘制两条路线和移动的小车
                    // 清除已经存在的轨迹
                    overlay.clear();
                    // 绘制新轨迹
                    overlay.addToMap(drivingRouteLineForPassenger, drivingRouteLineForDriver);
                    // 按照线路缩放地图
                    overlay.zoomToSpanPaddingBounds(200, 300, 200, 700);

                    // 小车移动
                    overlay.moveLooper(mMapView);

                    // 回到当前位置
                    BaiDuMapManager.baiDuMapManager.backToLocation();
                }
            }
        };

        // 添加监听器
        mSearch.setOnGetRoutePlanResultListener(listener);
        // 获得路线的起始和结束位置
        PlanNode stNode = PlanNode.withCityNameAndPlaceName(startMap.get("city"), startMap.get("place"));
        PlanNode enNode = PlanNode.withCityNameAndPlaceName(endMap.get("city"), endMap.get("place"));

        // 发起检索
        mSearch.drivingSearch((new DrivingRoutePlanOption())
                .from(stNode)
                .to(enNode));
    }

    // 绘制小车移动
    public void drawRouteAndCar2() {
        // 此时drvingRouteForPassenger是空的！需要保存用户订单的路径！！
        drivingRouteLineForPassenger = PassengerRouteLine.drivingRouteLine;
        // 同时在地图上绘制两条路线和移动的小车
        // 清除已经存在的轨迹
        overlay.clear();
        // 绘制新轨迹
        overlay.addCarToMap(drivingRouteLineForPassenger);
        // 按照线路缩放地图
        overlay.zoomToSpanPaddingBounds(200, 300, 200, 700);

        // 小车移动
        overlay.moveLooper2(mMapView);

        // 回到当前位置
        BaiDuMapManager.baiDuMapManager.backToLocation();
    }

    public void clear() {
        overlay.clear();
    }
}
